import {
  fetchUserCenter
} from '../../services/usercenter/fetchUsercenter';
import Toast from 'tdesign-miniprogram/toast/index';
import {
  getPerson
} from '@/services/usercenter/fetchPerson'

const menuData = [
  [{
      title: '企业名称',
      tit: '',
      key: 'userName',
      url: '',
      type: '',
    },
    {
      title: '统一社会信用代码',
      tit: '',
      key: 'creditCode',
      url: '',
      type: '',
    },
    {
      title: '联系人',
      tit: '',
      key: 'contact',
      url: '',
      type: '',
    },
    {
      title: '联系人电话',
      tit: '',
      key: 'contactMobile',
      url: '',
      type: '',
    },
    {
      title: '联系人邮箱',
      tit: '',
      key: 'email',
      url: '',
      type: '',
    },
    {
      title: '营业执照副本',
      tit: '',
      key: 'licenseUrl',
      url: '',
      type: 'image',
    },
    // {
    //   title: '申请平台供应商',
    //   tit: '孙姓者供应',
    //   url: '',
    //   type: '',
    // },
    {
      title: '注册时间',
      tit: '',
      key: 'registerTime',
      url: '',
      type: '',
    },
  ],
  [{
      title: '帮助中心',
      tit: '',
      url: '',
      type: 'help-center',
    },
    {
      title: '客服热线',
      tit: '17699369118',
      url: '',
      type: 'service',
      icon: 'service',
    },
  ],
];

const getDefaultData = () => ({
  showMakePhone: false,
  userInfo: {
    avatarUrl: '',
    nickName: '正在登录...',
    phoneNumber: '',
  },
  menuData,
  customerServiceInfo: {},
  currAuthStep: 1,
  showKefu: true,
  versionNo: '',
});

Page({
  data: getDefaultData(),

  onLoad() {
    this.getVersionInfo();
  },

  onShow() {
    const userInfo = wx.getStorageSync('userInfo') || {};
    if (Object.values(userInfo).length === 0) {
      Toast({
        context: this,
        selector: '#t-toast',
        message: '请先登录',
        icon: '',
        theme: 'warning',
        duration: 1000,
      });
      setTimeout(() => {
        wx.navigateTo({
          url: '/pages/auth/accountLogin/accountLogin',
        });
      }, 1000);
      this.setData({
        menuData: [],
      });
      return;
    }
    this.setData({
      userInfo,
    });
    this.getTabBar().init();
    this.init();
  },
  onPullDownRefresh() {
    this.init();
  },

  init() {
    this.fetUseriInfoHandle();
  },

  quit() {
    wx.clearStorageSync()
    wx.navigateTo({
      url: '/pages/auth/accountLogin/accountLogin',
    })
  },

  async fetUseriInfoHandle() {
    let userInfo = wx.getStorageSync('userInfo')
    userInfo = await getPerson({
      id: userInfo.id
    });

    wx.setStorageSync('userInfo', userInfo)

    menuData[0].forEach(item => {
      item.tit = userInfo[item.key] || item.tit
    })
    if (userInfo.userType === 1 || userInfo.isPrivate === 0) menuData[0] = menuData[0].filter(i => !['userName', 'creditCode', 'licenseUrl'].includes(i.key))

    if (userInfo.userType === 0 && userInfo.isPrivate === 1) menuData[0] = menuData[0].filter(menu => !['userName', 'licenseUrl'].includes(menu.key))

    this.setData({
      userInfo,
      menuData: userInfo.status ? menuData : [
        [{
          title: '企业信息审核中...',
          tit: '',
          key: 'userName',
          url: '',
          type: '',
        }],
        menuData[1]
      ],
      currAuthStep: 3,
    });
    wx.stopPullDownRefresh();

  },

  onClickCell({
    currentTarget
  }) {
    const {
      type
    } = currentTarget.dataset;
    return
    switch (type) {
      case 'address': {
        wx.navigateTo({
          url: '/pages/usercenter/address/list/index',
        });
        break;
      }
      case 'service': {
        this.openMakePhone();
        break;
      }
      case 'help-center': {
        Toast({
          context: this,
          selector: '#t-toast',
          message: '你点击了帮助中心',
          icon: '',
          duration: 1000,
        });
        break;
      }
      case 'point': {
        Toast({
          context: this,
          selector: '#t-toast',
          message: '你点击了积分菜单',
          icon: '',
          duration: 1000,
        });
        break;
      }
      case 'coupon': {
        wx.navigateTo({
          url: '/pages/coupon/coupon-list/index',
        });
        break;
      }
      default: {
        Toast({
          context: this,
          selector: '#t-toast',
          message: '未知跳转',
          icon: '',
          duration: 1000,
        });
        break;
      }
    }
  },

  jumpNav(e) {
    const status = e.detail.tabType;

    if (status === 0) {
      wx.navigateTo({
        url: '/pages/order/after-service-list/index',
      });
    } else {
      wx.navigateTo({
        url: `/pages/order/order-list/index?status=${status}`,
      });
    }
  },

  jumpAllOrder() {
    wx.navigateTo({
      url: '/pages/order/order-list/index',
    });
  },

  openMakePhone() {
    this.setData({
      showMakePhone: true,
    });
  },

  closeMakePhone() {
    this.setData({
      showMakePhone: false,
    });
  },

  call() {
    wx.makePhoneCall({
      phoneNumber: this.data.customerServiceInfo.servicePhone,
    });
  },

  gotoUserEditPage() {
    console.log(123)
    // const {
    //   currAuthStep
    // } = this.data;
    // if (currAuthStep === 2) {
    //   wx.navigateTo({
    //     url: '/pages/usercenter/person-info/index'
    //   });
    // } else {
    //   this.fetUseriInfoHandle();
    // }
    wx.navigateTo({
      url: '/pages/usercenter/person-info/index'
    });
  },

  getVersionInfo() {
    const versionInfo = wx.getAccountInfoSync();
    const {
      version,
      envVersion = __wxConfig
    } = versionInfo.miniProgram;
    this.setData({
      versionNo: envVersion === 'release' ? version : envVersion,
    });
  },
});